package com.jizhi.onework.controller;

import com.jizhi.onework.controller.common.BaseController;
import com.jizhi.onework.dataobj.vo.UserVO;
import com.jizhi.onework.framework.config.access.RequestOperation;
import com.jizhi.onework.framework.context.OperationContext;
import com.jizhi.onework.framework.model.Result;
import com.jizhi.onework.framework.model.ServiceResponse;
import com.jizhi.onework.pojo.User;
import com.jizhi.onework.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户门面接口
 *
 * @author hzhang7
 */
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 个人中心，需认证可访问
     */
    @RequestMapping("/user/index")
    @ResponseBody
    public String add(@RequestOperation OperationContext context, HttpServletRequest request){

        User bean = (User) SecurityUtils.getSubject().getPrincipal();
        request.setAttribute("userName", bean.getName());
        return "访问成功!";
    }

    /**
     * 个人中心，需认证可访问
     */
    @RequestMapping("/user/current")
    @ResponseBody
    public Result<UserVO> current(@RequestOperation OperationContext context, HttpServletRequest request){

        ServiceResponse<UserVO> response = userService.getUserById(context.getUser().getId());
        if (!response.isSuccess()) {
            return new Result<>(false, null, response.getError());
        }

        return convertResponseToResult(response);
    }
}
